# -*- coding: utf-8 -*-
# vim: set noet ts=4:
#
# scim-python
#
# Copyright (c) 2008-2008 Yu Yuwei <acevery@gmail.com>
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA
#
# $Id: $
#
import sys
reload (sys)
sys.setdefaultencoding('utf-8')

XingMa_Dict = {
	'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 
	'f':6, 'g':7, 'h':8, 'i':9, 'j':10,
	'k':11, 'l':12, 'm':13, 'n':14, 'o':15,
	'p':16, 'q':17, 'r':18, 's':19, 't':20,
	'u':21, 'v':22, 'w':23, 'x':24, 'y':25,
	'z':26, "'":27, ';':28, '`':29, '~':30, 
	'!':31, '@':32, '#':33, '$':34, '%':35,
	'^':36, '&':37, '*':38, '(':39, ')':40,
	'-':41, '_':42, '=':43, '+':44, '[':45,
	']':46, '{':47, '}':48, '|':49, '/':50,
	':':51, '"':52,  '<':53, '>':54, ',':55,
	'.':56, '?':57, '\\':58
	}

XingMa_List = XingMa_Dict.keys()

ID_XingMa_Dict = {}
for key,id in XingMa_Dict.items():
	ID_XingMa_Dict[id] = key

class XingMa_key:
	'''The class store'''
	def __init__(self, xm_key):
		try:
			if xm_key not in XingMa_List:
				error_m = u'%s is not in XingMa_Dict' % xm_key
				raise Exception ( error_m.encode('utf8') )
		except Exception, e:
			print e
			import traceback
			traceback.print_exc ()
		self._key = xm_key
		self._key_id = XingMa_Dict[xm_key]
	
	def get_key_id(self):
		return self._key_id

	def get_key(self):
		return self._key

	def __str__(self):
		return self._key

	def __int__(self):
		return self._key_id

def Parse ( inputstr ):
	
	ids_input = []
	
	for i in inputstr:
		try:
			ids_input.append( XingMa_key(i) )
		except:
			pass
	
	return ids_input[:]

def Deparse (id):
	'''deparse the id code of XingMa, id could be int or int in string form'''
	if id:
		id = int(id)
		if id in ID_XingMa_Dict:
			return ID_XingMa_Dict[id]
	else:
		return ''
